<template>
  <div class="department">
    <page-search
      :searchFormConfig="searchFormConfig"
      @resetBtnClick="handleResetBtn"
      @searchBtnClick="handleSearchBtn"
    ></page-search>
    <page-content
      :contentTableConfig="contentTableConfig"
      @createBtnClick="handleCreateData"
      @updateBtnClick="handleUpdateData"
      pageName="department"
      ref="pageContentRef"
    ></page-content>
    <page-modal
      pageName="department"
      ref="pageModalRef"
      :defaultInfo="defaultInfo"
      :modalConfig="modalConfigComputed"
    ></page-modal>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed } from 'vue'

import { searchFormConfig } from './config/search-config'
import { contentTableConfig } from './config/content-config'
import { modalDialogConfig } from './config/modal-config'

import { usePageSearch } from '@/hooks/usePageSearch'
import { usePageModal } from '@/hooks/usePageModal'

import { useStore } from '@/store'

export default defineComponent({
  name: 'department',
  setup() {
    const [pageContentRef, handleResetBtn, handleSearchBtn] = usePageSearch()

    const [pageModalRef, defaultInfo, handleCreateData, handleUpdateData] =
      usePageModal()

    // 动态添加部门列表
    const modalConfigComputed = computed(() => {
      const store = useStore()
      const departmentItem = modalDialogConfig.formItems?.find(
        (item) => item.field === 'parentId'
      )
      departmentItem!.options = store.state.entireDepartment.map((item) => {
        return { title: item.name, value: item.id }
      })
      return modalDialogConfig
    })

    return {
      searchFormConfig,
      contentTableConfig,
      modalConfigComputed,
      pageContentRef,
      handleResetBtn,
      handleSearchBtn,
      pageModalRef,
      defaultInfo,
      handleCreateData,
      handleUpdateData
    }
  }
})
</script>

<style scoped></style>
